var id="0021500874";




var gh = (function() {
    var video=document.createElement('div');

    var user_info_div;
  return {
    onload: function () {
        game_Button_div = document.querySelector('#game_Button');
        user_info_div = document.querySelector('#user_info');
        video_info_div = document.querySelector('#video_info');
        user_info_div.setAttribute("id","info")
        video_info_div.setAttribute("id","videoInfo")




        var g1= document.getElementById("g1");
        g1.onclick=game1;
        var g2= document.getElementById("g2");
        g2.onclick=game2;
        jing();
        xia();
        jingjing()
   }
  };


    function jingjing() {
        qixia('GET', "http://101.34.184.45/getNbaSchedules",
            false, qiqi);
    }
    function qiqi(error, status, response) {

        if (!error && status === 200) {
            var data =JSON.parse(response);
            var len=data["GameList"].length;
            for (var i=0;i<len;i++){
                var button = document.createElement('button');
                  button.setAttribute("class","box");
                  button.setAttribute("id",data["GameList"][i]["GameId"]);
                  button.innerHTML = data["GameList"][i]["HomeTeam"]["DisplayAbbr"]+"---"+
                  data["GameList"][i]["AwayTeam"]["DisplayAbbr"];
                button.addEventListener("click",function (event) {
                    changeGame(this.id);
                });
                game_Button_div.append(button);
            }


        }
    }
    function changeGame(gameId) {
        chrome.storage.local.set({gameId: gameId}, function() {
            document.getElementById('info').innerHTML = '';
            document.getElementById('videoInfo').innerHTML = '';
            chrome.storage.local.set({desc: "暂无视频"}, function() {
            });
            chrome.storage.local.set({key: "null"}, function() {
            });
            jing();
            xia();
        });

    }
    function game1() {
        chrome.storage.local.set({gameId: "0021500874"}, function() {
            document.getElementById('info').innerHTML = '';
            document.getElementById('videoInfo').innerHTML = '';
            chrome.storage.local.set({desc: "库里超远三分半场绝杀雷霆"}, function() {
            });
            chrome.storage.local.set({key: "curry"}, function() {
            });
            jing();
            xia();
        });

    }
    function game2() {
        chrome.storage.local.set({gameId: "0021800565"}, function() {
            document.getElementById('info').innerHTML = '';
            document.getElementById('videoInfo').innerHTML = '';
            chrome.storage.local.set({desc: "哈登逆天三分绝杀勇不出界！"}, function() {
            });
            chrome.storage.local.set({key: "harden"}, function() {
                jing();
            });
            xia();

        });

    }
    function jing() {
        chrome.storage.local.get(['key'], function(result) {
            if (result.key===undefined){
                chrome.storage.local.set({key: "curry"}, function() {
                });
            }
            video.innerHTML="<video src='/images/"+result.key+".mp4' width='350' height='200' loop autoplay></video> ";
            video_info_div.appendChild(video);

        });
    }
function xia() {
    // var url='http://101.34.184.45/getNbaGame/';
    var url='http://101.34.184.45/getNbaGame/';

    chrome.storage.local.get(['gameId'], function (result) {
        if (result.gameId===undefined){
                url=url+"0022100904";
            chrome.storage.local.set({key: "0022100904"}, function() {
            });
            qixia('GET', url,
                false, qi);
        }else {
            url=url.concat(result.gameId);
            qixia('GET', url,
                false, qi);
        }

    });

}
function qixia(method, url, interactive, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open(method, url);
  xhr.onload = requestComplete;
  xhr.send();
function requestComplete() {
  // console.log('requestComplete', this.status, this.response);
  if ( ( this.status < 200 || this.status >=300 ) ) {
  } else {
    callback(null, this.status, this.response);
  }
}
}
function qi(error, status, response) {
  if (!error && status == 200) {
      var nba_info = JSON.parse(response);
      var elem = user_info_div;
      addGameDetails(elem,nba_info);

      for (var i = 0; i <8; i++) {
              var description = nba_info["payload"]["playByPlays"][0]["events"][i]["description"];
              var nameElem = document.createElement('div');
              nameElem.innerHTML = "<b> " + description + "</b>";
              elem.appendChild(nameElem);
          }

  }

}

    function  addGameDetails(elem,nba_info){
        var homeScore=nba_info["payload"]["boxscore"]["homeScore"];
        var awayScore=nba_info["payload"]["boxscore"]["awayScore"];
        var statusDesc=nba_info["payload"]["boxscore"]["statusDesc"];
        var periodClock=nba_info["payload"]["boxscore"]["periodClock"];
        var awayTeamNameEn  =nba_info["payload"]["awayTeam"]["profile"]["nameEn"];
        var homeTeamNameEn  =nba_info["payload"]["homeTeam"]["profile"]["nameEn"];
        var awayTeamName  =nba_info["payload"]["awayTeam"]["profile"]["name"];
        var homeTeamName  =nba_info["payload"]["homeTeam"]["profile"]["name"];
        var nameEn=document.createElement('div');
        var name=document.createElement('div');
        var score=document.createElement('div');
        var des=document.createElement('div');

        nameEn.innerHTML="<H2 id='blue'>"+awayTeamNameEn + " VS " +homeTeamNameEn +"</H2>";
        name.innerHTML="<H2 id='green'>"+awayTeamName + " VS " +homeTeamName +"</H2>";
        score.innerHTML="<H2 id='pink'>"+awayScore + " VS " +homeScore +"  "+statusDesc+"    剩余时间:"+periodClock+"</H2>";
        chrome.storage.local.get(['desc'], function(result) {
            des.innerHTML="<H2 id='red'>"+result.desc+ "</H2>";
        });

        elem.appendChild(nameEn);
        elem.appendChild(name);
        elem.appendChild(score);
        elem.appendChild(des);
        elem.appendChild(video);

    }


})();




window.onload = gh.onload;